import datetime

from django.http import JsonResponse
from django.shortcuts import render, redirect, HttpResponse
from web import models




def Gindex(request):

        user_mobile_phone = request.session.get('user_mobile_phone', 0)  # 没有就user_id为0
        # 获取session中的user_id,如果用户登陆了，坑等有，如果没有登陆，就没有
        user_object = models.guser.objects.filter(mobile_phone=user_mobile_phone).first()
        if user_object:  # 木有登陆正确的账号
                question_object = models.question.objects.filter(org_id=user_object.org_id).first()  # 查问题表，看有没有创建问题表
                date1 = datetime.date.today()
                start = date1.strftime("%Y-%m-%d") + " 00:00:00"
                end = date1.strftime("%Y-%m-%d") + " 23:59:59"
                starttime = datetime.datetime.strptime(start, "%Y-%m-%d %H:%M:%S")
                endtime = datetime.datetime.strptime(end, "%Y-%m-%d %H:%M:%S")

                """获取IP地址"""
                x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')  # 判断是否使用代理
                if x_forwarded_for:
                    ip = x_forwarded_for.split(',')[0]  # 使用代理获取真实的ip
                else:
                    ip = request.META.get('REMOTE_ADDR')  # 未使用代理获取IP


                clock_object=models.clock.objects.filter(org_id=user_object.org_id).filter(job_id=user_object.job_id).order_by("-nowdate").first()
                if clock_object:
                    if clock_object.nowdate>=starttime and clock_object.nowdate<=endtime:#今天重复打卡
                        state=True
                    if clock_object.nowdate<=starttime: #今天没有打卡
                        state = False
                else:
                    state = False
                if request.method == "GET":
                     if question_object:  # 如果有的话返回问题导图页面上
                                    qulist = []
                                    list2 = []
                                    if question_object.question1:
                                                            qulist.append(question_object.question1)
                                    if question_object.question2:
                                                            qulist.append(question_object.question2)
                                    if question_object.question3:
                                                            qulist.append(question_object.question3)
                                    if question_object.question4:
                                                            qulist.append(question_object.question4)
                                    if question_object.question5:
                                                            qulist.append(question_object.question5)
                                    if question_object.question6:
                                                            qulist.append(question_object.question6)
                                    if question_object.question7:
                                                            qulist.append(question_object.question7)
                                    if question_object.question8:
                                                            qulist.append(question_object.question8)
                                    if question_object.question9:
                                                            qulist.append(question_object.question9)
                                    if question_object.question10:
                                                            qulist.append(question_object.question10)
                                    if question_object.question11:
                                                            qulist.append(question_object.question11)
                                    for i in range(1, 13):
                                             list2.append(i)

                                    dic_new = dict(zip(list2, qulist))
                                    print(dic_new)

                                    return render(request, 'web/Gindex.html', {"qulist": dic_new,"state":state},)
                     else:
                                    return render(request, 'web/Gindex.html', {"qulist": False,"state":False})

                else:


                     if clock_object and state == True: #今天打了卡

                            clock_object.answer1 = request.POST.get("1")

                            clock_object.answer2 = request.POST.get("2")

                            clock_object.answer3 = request.POST.get("3")

                            clock_object.answer4 = request.POST.get("4")

                            clock_object.answer5 = request.POST.get("5")

                            clock_object.answer6 = request.POST.get("6")

                            clock_object.answer7 = request.POST.get("7")

                            clock_object.answer8 = request.POST.get("8")

                            clock_object.answer9 = request.POST.get("9")

                            clock_object.answer10 = request.POST.get("10")

                            clock_object.answer11 = request.POST.get("11")

                            clock_object.ip=ip
                            clock_object.save()
                            return JsonResponse({"status": True})
                     else:
                         try:
                             models.clock.objects.create(

                                 org_id=user_object.org_id,
                                 job_id=user_object.job_id,
                                 ip=ip,
                                 answer1=request.POST.get("1"),
                                 answer2=request.POST.get("2"),
                                 answer3=request.POST.get("3"),
                                 answer4=request.POST.get("4"),
                                 answer5 = request.POST.get("5"),
                                 answer6 = request.POST.get("6"),
                                 answer7 = request.POST.get("7"),
                                 answer8 = request.POST.get("8"),
                                 answer9 = request.POST.get("9"),
                                 answer10 = request.POST.get("10"),
                                 answer11 = request.POST.get("11"),
                             )
                             return JsonResponse({"status": True})
                         except Exception :
                             return JsonResponse({"status": False})

        else:
            return redirect('/web/login/')
